<?php

namespace app\admin\controller\general;

use app\admin\model\Admin;
use app\admin\model\Store;
use app\api\model\SeparateSet;
use app\common\controller\Backend;
use fast\Random;
use think\Request;
use think\Session;
use think\Validate;
use wechat\MiniProgramService;

/**
 * 个人配置
 *
 * @icon fa fa-user
 */
class Setting extends Backend
{

    protected $searchFields = 'id,title';

    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this->model = new \app\api\model\SeparateSet();
        $this->service_secret = Session::get('admin')['secret'];
        if (empty($this->service_secret))
            $this->service_secret = "Jp)GuX=M^KthZV7^";
    }
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);

        $secret = SeparateSet::get(['secret'=>$this->service_secret]);
        $this->wechat = new MiniProgramService($secret);
        $store = Store::get(['service_secret'=>$this->service_secret]);
        $res = $this->wechat->app_code(['path'=>'pages/getopenid/index',
            'directory'=>"/path/to/openid/",
            'sub_mch_id'=>$store->sub_mch_id]);
        $path = "";
        if ($res['result_code'] == "SUCCESS")
            $path = $res['data'];
        $result = $this->model->where(['secret'=>$this->service_secret])->find();
        $this->assign('setting',$result);
        $this->assign("path",$path);
        return $this->view->fetch();
    }
    public function edit($ids = null){
        $params = $this->request->param("row/a");
        SeparateSet::update(['commission'=>$params['commission']],['secret'=>$this->service_secret]);
        $this->success("修改成功");
    }
}
